﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AllInOne.PrintUtils
{
    public class MyWebBrowser : WebBrowser
    {
        public event MyWebBrowser.DocHostShowUIShowMessageEventHandler WBDocHostShowUIShowMessage = null;

        protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
        {
            return new MyWebBrowser.ExtendedWebBrowserSite(this);
        }
        
        public delegate void DocHostShowUIShowMessageEventHandler(object sender, ExtendedBrowserMessageEventArgs e);

        private class ExtendedWebBrowserSite : WebBrowser.WebBrowserSite, UnsafeNativeMethods.IDocHostShowUI
        {
            public ExtendedWebBrowserSite(MyWebBrowser host) : base(host)
            {
                this._Browser = host;
            }

            int UnsafeNativeMethods.IDocHostShowUI.ShowMessage(IntPtr hwnd, string lpstrText, string lpstrCaption, uint dwType, string lpstrHelpFile, uint dwHelpContext, ref int lpResult)
            {
                int result = 1;
                if (this._Browser.WBDocHostShowUIShowMessage != null)
                {
                    ExtendedBrowserMessageEventArgs extendedBrowserMessageEventArgs = new ExtendedBrowserMessageEventArgs(hwnd, lpstrText, lpstrCaption, dwType, lpstrHelpFile, dwHelpContext);
                    this._Browser.WBDocHostShowUIShowMessage(this, extendedBrowserMessageEventArgs);
                    if (extendedBrowserMessageEventArgs.Cancel)
                    {
                        result = 0;
                        lpResult = extendedBrowserMessageEventArgs.pResult;
                    }
                }
                return result;
            }

            int UnsafeNativeMethods.IDocHostShowUI.ShowHelp(IntPtr hwnd, string pszHelpFile, uint uCommand, uint dwData, UnsafeNativeMethods.tagPOINT ptMouse, object pDispatchObjectHit)
            {
                return -2147467263;
            }

            private MyWebBrowser _Browser;
        }
    }
}
